﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WF.Model;
using WF.DAL;

namespace WF.BusinessObject
{
    public class NameValuePair : BOBase<NameValuePairEntity, DAL.NameValuePair>
    {
        #region 构造函数

        /// <summary>
        /// 构造函数

        /// </summary>
        public NameValuePair() : base() { }

        /// <summary>
        /// 构造函数

        /// </summary>
        /// <param name="DataLayer"></param>
        public NameValuePair(WFDBDataContext DataContext) : base(DataContext) { }
        #endregion

        #region 实现基类abstract方法
        /// <summary>
        /// 实现基类的GetById abstract方法
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public override NameValuePairEntity GetById(string ID)
        {
            var obj = DataContext.NameValuePairs.SingleOrDefault(o => o.ID == ID);
            return new NameValuePairEntity(obj);
        }
        #endregion

        #region Public 方法
        /// <summary>
        /// 返回所有的NameValuePair
        /// </summary>
        /// <returns></returns>
        public List<NameValuePairEntity> GetAll()
        {
            //获得指定类别的NameValuePairs，并按Category, SortOrder排序
            var objs = DataContext.NameValuePairs.OrderBy(obj => obj.Category).OrderBy(obj => obj.SortOrder);

            //填充返回的列表



            List<NameValuePairEntity> _result = new List<NameValuePairEntity>();
            foreach (var item in objs)
            {
                _result.Add(new NameValuePairEntity(item));
            }
            return _result;
        }

        /// <summary>
        /// 根据Category返回NameValuePair
        /// </summary>
        /// <param name="Category"></param>
        /// <returns></returns>
        public List<NameValuePairEntity> GetNameValuePairEntityByCategory(string Category)
        {
            return GetNameValuePairEntityByCategory(Category, false);
        }

        /// <summary>
        /// 根据Category返回NameValuePair
        /// </summary>
        /// <param name="Category"></param>
        /// <returns></returns>
        public List<NameValuePairEntity> GetNameValuePairEntityByCategory(string Category, bool includeAll)
        {
            //获得指定类别的NameValuePairs，并按SortOrder排序
            var objs = DataContext.NameValuePairs.Where(obj => obj.Category == Category).OrderBy(obj => obj.SortOrder);

            //填充返回的列表


            List<NameValuePairEntity> _result = new List<NameValuePairEntity>();
            foreach (var item in objs)
            {
                _result.Add(new NameValuePairEntity(item));
            }
            if (includeAll)
            {
                NameValuePairEntity item = new NameValuePairEntity();
                item.Name = "--所有--";
                item.Value = "9999";
                _result.Insert(0, item);
            }
            return _result;
        }

        /// <summary>
        /// 根据Category返回NameValuePair
        /// </summary>
        /// <param name="Category"></param>
        /// <returns></returns>
        public List<NameValuePairEntity> GetNameValuePairEntityByCategory(string Category, bool includeBlankItem, string blankItemText, string blankItemValue)
        {
            //获得指定类别的NameValuePairs，并按SortOrder排序
            var objs = DataContext.NameValuePairs.Where(obj => obj.Category == Category).OrderBy(obj => obj.SortOrder);

            //填充返回的列表



            List<NameValuePairEntity> _result = new List<NameValuePairEntity>();
            foreach (var item in objs)
            {
                _result.Add(new NameValuePairEntity(item));
            }
            if (includeBlankItem)
            {
                if (string.IsNullOrEmpty(blankItemText))
                {
                    blankItemText = "--请选择--";
                }
                if (string.IsNullOrEmpty(blankItemValue))
                {
                    blankItemValue = "";
                }
                NameValuePairEntity item = new NameValuePairEntity();
                item.Name = blankItemText;
                item.Value = blankItemValue;
                _result.Insert(0, item);
            }
            return _result;
        }

        #endregion

        #region NameValuePair
        public bool GetAllNameValuePairEntity(out List<NameValuePairEntity> outlist)
        {

            //获得指定类别的NameValuePairs，并按Category, SortOrder排序
            IQueryable<DAL.NameValuePair> NameValuePairs = DataContext.NameValuePairs.OrderBy(obj => obj.Category).OrderBy(obj => obj.SortOrder);

            //填充返回的列表





            outlist = new List<NameValuePairEntity>();
            foreach (var item in NameValuePairs)
            {
                outlist.Add(new NameValuePairEntity(item));
            }


            return true;
        }

        public bool GetNameValuePairEntityByCategory(string Category, out List<NameValuePairEntity> outlist)
        {

            //获得指定类别的NameValuePairs，并按SortOrder排序
            IQueryable<DAL.NameValuePair> NameValuePairs = DataContext.NameValuePairs.Where(obj => obj.Category == Category).OrderBy(obj => obj.SortOrder);

            //填充返回的列表





            outlist = new List<NameValuePairEntity>();
            foreach (var item in NameValuePairs)
            {
                outlist.Add(new NameValuePairEntity(item));
            }

            return true;
        }

        ///// <summary>
        ///// 根据ID获取
        ///// </summary>
        ///// <param name="ID"></param>
        ///// <param name="nameValuePairEntity"></param>
        ///// <returns></returns>
        //public bool GetNameValuePairEntityByID(string ID, out NameValuePairEntity nameValuePairEntity)
        //{

        //    DAL.NameValuePair nameValuePair = DataContext.NameValuePairs.SingleOrDefault(obj => obj.ID == ID);
        //    nameValuePairEntity = new NameValuePairEntity(nameValuePair);
        //    return true;
        //}

        ///// <summary>
        ///// 新增
        ///// </summary>
        ///// <param name="entity"></param>
        ///// <returns></returns>
        //public bool Add(NameValuePairEntity entity)
        //{

        //    DAL.NameValuePair obj = entity.DBEntity;
        //    DataContext.NameValuePairs.InsertOnSubmit(obj);
        //    DataContext.SubmitChanges();

        //    return true;
        //}

        ///// <summary>
        ///// 删除
        ///// </summary>
        ///// <param name="entity"></param>
        ///// <returns></returns>
        //public bool Delete(NameValuePairEntity entity)
        //{
        //    DAL.NameValuePair obj = entity.DBEntity;
        //    DataContext.NameValuePairs.DeleteOnSubmit(obj);
        //    DataContext.SubmitChanges();

        //    return true;
        //}

        ///// <summary>
        ///// 更新
        ///// </summary>
        ///// <param name="entity"></param>
        ///// <returns></returns>
        //public bool Update(NameValuePairEntity entity)
        //{
        //    DAL.NameValuePair nameValuePair = DataContext.NameValuePairs.SingleOrDefault(obj => obj.ID == entity.ID);
        //    NameValuePairEntity.UpdateDbFromEntity(entity, nameValuePair);
        //    DataContext.SubmitChanges();

        //    return true;
        //}

        #endregion
    }

    /// <summary>
    /// NameValuePair的类别

    /// </summary>
    public class NameValueCategory
    {
        public const string Sex = "性别";
        public const string Nationality = "民族";
        public const string EmpType = "职员类型";
        public const string EmpStatus = "职员状态";
        public const string Education = "学历";
        public const string NativePlace = "籍贯";
        public const string JobGrade = "职等";
    }

}
